﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace TestConnection
{
    public class ViewIP
    {

        DataGridView dataGridViewIP { get; set; }
        ProgressBar toolStripProgressBar { get; set; }
        Label toolStripStatusLabel { get; set; }
        
        public ViewIP() { }

        public ViewIP(DataGridView dataGridViewIP, ProgressBar toolStripProgressBar, Label toolStripStatusLabel)
        {
            // TODO: Complete member initialization
            this.dataGridViewIP = dataGridViewIP;
            this.toolStripProgressBar = toolStripProgressBar;
            this.toolStripStatusLabel = toolStripStatusLabel;
           
        }

       public int stt = 0;
        public int valuesPro = 0;
        public string GetCommandCmd(string cmd)
        {
            System.Diagnostics.ProcessStartInfo procStartInfo =
                   new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmd);
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = true;
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;
            proc.Start();
            string result = proc.StandardOutput.ReadToEnd();
            return result;
        }

        public delegate void setData(string ip, string name);
        public void setValues(string ip, string name)
        {
           dataGridViewIP.Rows[stt].Cells[0].Value = ip;
           dataGridViewIP.Rows[stt].Cells[1].Value = name;
            stt++;
        }


        public void setValuestoolStripProgressBar(string values, string name)
        {
            if (Convert.ToInt32(values) == -1)
            {
                valuesPro = toolStripProgressBar.Maximum;
                toolStripStatusLabel.Text = "Finish!";
            }
            toolStripProgressBar.Value = valuesPro;
            valuesPro++;
            if (valuesPro > toolStripProgressBar.Maximum)
                valuesPro = 1;
        }

        public void FindIpLan()
        {
            string ip = GetCommandCmd("ipconfig");
            Regex re = new Regex("IP.*? Address.*?[:] ([0-9]+.[0-9]+.[0-9]+.[0-9]+)");
            MatchCollection match = re.Matches(ip);

            if (match.Count > 0)
            {
                ip = "";
                foreach (Match m in match)
                {
                    ip = m.Groups[1].Value.ToString();
                    string macC = "";
                    int pos = ip.Length - 1;
                    while (pos > 0 && ip[pos] != '.')
                    {
                        pos--;
                    }
                    macC = ip.Substring(0, pos + 1);
                    for (int i = 0; i < 254; i++)
                    {
                        ip = macC + i.ToString();
                        string ret = GetCommandCmd("ping " + ip + " -n 1 -w 100");
                        Regex re1 = new Regex("Reply from ([0-9]+.[0-9]+.[0-9]+.[0-9]+)");
                        Match ma = re1.Match(ret);
                        if (ma.Success)
                        {
                            try
                            {
                                IPHostEntry hostInfo = Dns.GetHostByAddress(ip);
                                dataGridViewIP.Invoke(new setData(setValues), ip, hostInfo.HostName);

                            }
                            catch (Exception ex)
                            {

                            }


                        }
                       
                        dataGridViewIP.Invoke(new setData(setValuestoolStripProgressBar), "0", "0");
                    }

                }
            }
            dataGridViewIP.Invoke(new setData(setValuestoolStripProgressBar), "-1", "");
        }


        public void loadIP()
          {
             toolStripProgressBar.Maximum = 20;
             toolStripProgressBar.Minimum = 0;
             toolStripProgressBar.Value = 5;
             dataGridViewIP.RowCount = 10;
             Thread t = new Thread(new ThreadStart(FindIpLan)); 
             t.Start();
         
          }


         
    }
}
